<?php

namespace Admin\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Admin\Model\Product;
use Admin\Form\ProductForm;

class ProductController extends AbstractActionController
{
	/* @var $productTable \Admin\Model\ProductTable */
	protected $productTable;
	protected $categoryTable;
	
	public function indexAction() {		
		return new ViewModel(array(
			'products'	=> $this->getProductTable()->fetchAll(),
		));
	}
	
	private function initSelect(&$form)
	{
		$categoryTable = $this->getCategoryTable();
		$categories = $categoryTable->fetchAll();
		$options = array();
		foreach($categories as $category) $options[$category->id] = $category->name;
		
		$form->add(array(
			'type' => 'Zend\Form\Element\Select',
             'name' => 'p_id',
             'options' => array(
                     'label' => 'Category',
                     'value_options' => $options
             )
		));
	}
	
	public function addAction()
	{
		$form = new ProductForm();
		$this->initSelect($form);
		$form->get('submit')->setValue('Add');
		
		$request = $this->getRequest();
		
		if($request->isPost()){
			$product = $this->getProduct();
			$form->setInputFilter($product->getInputFilter());
			$form->setData($request->getPost());
			
			if($form->isValid()){
				$product->exchangeArray($form->getData());
				$this->getProductTable()->save($product);
				return $this->redirect()->toRoute('product');
			}else{
				pr($form->getData());
				pr($form->getMessages());die;
			}
		}
		
		return array('form'	=> $form);
	}

	public function editAction()
	{
		$id = (int)$this->params()->fromRoute('id',0);
		if(!$id){
			return $this->redirect()->toRoute('product',array(
				'action'	=> 'add'
			));
		}
		
		try {
			$product = $this->getProductTable()->get($id);
		}
		catch(\Exception $ex)
		{
			return $this->redirect()->toRoute('product',array(
				'action'	=> 'index'
			));
		}
		
		$form = new ProductForm();
		$form->bind($product);
		$this->initSelect($form);
		$form->get('submit')->setAttribute('value', 'Edit');
		
		$request = $this->getRequest();
		if($request->isPost()){
			$form->setInputFilter($product->getInputFilter());
			$form->setData($request->getPost());
			
			if($form->isValid()){
				$this->getProductTable()->save($form->getData());
				
				return $this->redirect()->toRoute('category');
			}
		}
		
		return array(
			'id'	=> $id,
			'form'	=> $form,
		);
	}
	
	public function deleteAction() {
		$id = (int) $this->params()->fromRoute('id', 0);
		if (!$id) {
			return $this->redirect()->toRoute('product');
		}

		$request = $this->getRequest();
		if ($request->isPost()) {
			$del = $request->getPost('del', 'No');

			if ($del == 'Yes') {
				$id = (int) $request->getPost('id');
				$this->getProductTable()->delete($id);
			}

			return $this->redirect()->toRoute('product');
		}

		return array(
			'id' => $id,
			'pro' => $this->getProductTable()->get($id)
		);
	}
	
	public function getCategoryTable() {
		if (!$this->categoryTable) {
			$sm = $this->getServiceLocator();
			$this->categoryTable = $sm->get('Admin\Model\CategoryTable');
		}
		return $this->categoryTable;
	}
	
	public function getProduct()
	{
		$sm = $this->getServiceLocator();
		return $sm->get('Admin\Model\Product');
	}
	
	public function getProductTable()
	{
		if(!$this->productTable)
		{
			$sm = $this->getServiceLocator();
			$this->productTable = $sm->get('Admin\Model\ProductTable');
		}
		return $this->productTable;
	}
}